package com.gitee.somenvm.ninerpgitem.core.command

import com.gitee.somenvm.ninerpgitem.api.ItemApi
import com.gitee.somenvm.ninerpgitem.api.ItemApi.isBind
import com.gitee.somenvm.ninerpgitem.core.item.ItemLoader
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.CommandBody
import taboolib.common.platform.command.CommandHeader
import taboolib.common.platform.command.mainCommand
import taboolib.common.platform.command.subCommand
import taboolib.common.platform.function.getProxyPlayer
import taboolib.common5.Coerce
import taboolib.expansion.createHelper
import taboolib.module.lang.sendLang
import taboolib.module.nms.getName
import taboolib.platform.util.isNotAir
import taboolib.platform.util.sendLang

/**
 * BY NineRpgItem
 * com.gitee.somenvm.ninerpgitem.core.command
 *
 * @author  SomeNvM
 * @date  2023/1/19 16:11
 */
@CommandHeader(
    name = "rpgitem",
    aliases = ["ri", "ni"],
    permission = "ninerpgitem.item"
)
object ItemCommand {
    @CommandBody
    val main = mainCommand {
        createHelper()
    }

    @CommandBody(permission = "NineRpgItem.item.get")
    val get = subCommand {
        dynamic(comment = "item") {
            suggestion<Player> { _, _ -> ItemLoader.configNitems.map { it.id } }
            // 1
            execute<Player> { sender, _, argument ->
                ItemApi.givePlayerNineItem(sender, argument)
                sender.sendLang("get-item", argument, 1)
            }
            // 自定义数量
            dynamic(optional = true, comment = "amount") {
                execute<Player> { sender, context, argument ->
                    var amount = Coerce.toInteger(argument)
                    if (amount == 0) amount = 1
                    val id = context["item"]
                    ItemApi.givePlayerNineItem(sender, id, amount)
                    sender.sendLang("get-item", id, amount)
                }
            }
        }
    }

    @CommandBody(permission = "NineRpgItem.item.give")
    val give = subCommand {
        dynamic(comment = "player") {
            suggestion<ProxyCommandSender> { _, _ -> Bukkit.getOnlinePlayers().map { it.name } }

            dynamic(comment = "item") {
                suggestion<ProxyCommandSender> { _, _ -> ItemLoader.configNitems.map { it.id } }
                execute<ProxyCommandSender> { sender, context, id ->
                    val player = getProxyPlayer(context["player"])
                    if (player != null) {
                        ItemApi.givePlayerNineItem(player.cast(), id)
                        if (player.cast<Player>() != sender)
                            sender.sendLang("give-item", id, 1, player.name)
                        player.sendLang("get-item", id, 1)
                    } else {
                        sender.sendLang("no-player", context["player"])
                    }
                }

                dynamic(optional = true, comment = "amount") {
                    execute<ProxyCommandSender> { sender, context, argument ->
                        val player = getProxyPlayer(context["player"])
                        val id = context["item"]
                        var amount = Coerce.toInteger(argument)
                        if (amount == 0) amount = 1
                        if (player != null) {
                            player.cast<Player>().isDead
                            ItemApi.givePlayerNineItem(player.cast(), id, amount)
                            if (player.cast<Player>() != sender)
                                sender.sendLang("give-item", id, amount, player.name)
                            player.sendLang("get-item", id, amount)
                        } else {
                            sender.sendLang("no-player", context["player"])

                        }
                    }
                }
            }
        }

    }

    @CommandBody
    val update = subCommand {

        execute<ProxyPlayer> { sender, _, _ ->
            val hand = sender.cast<Player>().inventory.itemInMainHand
            if (hand.isNotAir()) {
                sender.cast<Player>().inventory.setItemInMainHand(ItemApi.updateItem(hand, sender.cast()))
                sender.sendLang("update-item", hand.getName(sender.cast()))
            } else {
                sender.sendLang("hand-no-item")
            }
        }
    }

    @CommandBody(permission = "NineRpgItem.item.bind")
    val bind = subCommand {
        execute<ProxyPlayer> { sender, _, _ ->
            val hand = sender.cast<Player>().inventory.itemInMainHand
            if (hand.isNotAir()) {
                if (!hand.isBind()) {
                    sender.cast<Player>().inventory.setItemInMainHand(ItemApi.bindItem(hand, sender.cast()))
                    sender.sendLang("bind-item", hand.getName(sender.cast()))
                } else {
                    sender.sendLang("item-is-bind", hand.getName(sender.cast()))
                }
            } else {
                sender.sendLang("hand-no-item", hand.getName(sender.cast()))
            }
        }
    }

    @CommandBody(permission = "NineRpgItem.item.edit")
    val edit = subCommand {
        execute<ProxyCommandSender> { sender, _, _ ->
            sender.sendMessage("§c未完成")
        }
    }

    @CommandBody(permission = "NineRpgItem.item.create")
    val create = subCommand {
        execute<ProxyCommandSender> { sender, _, _ ->
            sender.sendMessage("§c未完成")
        }
    }
}